<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog :title="title" v-model="openSearch" :close-on-click-modal="false" draggable width="500px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
        <el-col :span="24">
          <el-form-item label="月份">
            <el-date-picker
              style="width: 300px"
              v-model="date"
              :type="'month'"
              :format="'yyyy-MM'"
              autocomplete="off"
              :clearable="false"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="销售单号" prop="marketingOrder">
            <el-input v-model="queryParams.marketingOrder" placeholder="请输入销售单号" clearable style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="制造单号" prop="manufacturingOrder">
            <el-input v-model="queryParams.manufacturingOrder" placeholder="请输入制造单号" clearable style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="param">
            <el-input v-model="queryParams.param" placeholder="请输入材料物料代码或品名规格" clearable style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="成品综合查询" prop="completeParam">
            <el-input v-model="queryParams.completeParam" placeholder="请输入物料代码或成品编码或品名规格" clearable style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-top: 25px; display: flex; justify-content: flex-end">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" @click="handleSearch" size="small">查询</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExportDetail"
          :loading="exportLoading1"
          size="small">
          明细数据
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExportTotal1"
          :loading="exportLoading2"
          size="small">
          注塑车间
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExportTotal2"
          :loading="exportLoading3"
          size="small">
          喷涂车间
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExportTotal3"
          :loading="exportLoading4"
          size="small">
          丝印车间
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 5px">
        <Page
          v-if="activeTab === 'first'"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total1"
          @on-change="pageChange1"
          simple></Page>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </el-row>

    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          :height="tableH"
          :data="materialSavingDetail"
          v-loading="loading1"
          :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="#" type="index" width="50" align="center" fixed="left" />
          <el-table-column label="营销单号" align="center" prop="somDjbh" width="120" />
          <el-table-column label="物料代码" align="center" prop="somWldm" width="120" />
          <el-table-column label="成品编码" :show-overflow-tooltip="true" align="center" prop="somCustwldm" width="120" />
          <el-table-column label="成品品名规格" :show-overflow-tooltip="true" align="center" prop="somPmgg" width="280" />
          <el-table-column label="订单数量" align="center" prop="somDdsl" width="80" />
          <el-table-column label="制造单号" align="center" prop="moeZzdh" width="160" />
          <el-table-column label="物料代码" align="center" prop="moeWldm" width="120" />
          <el-table-column label="产品编码" :show-overflow-tooltip="true" align="center" prop="moeCustwldm" width="120" />
          <el-table-column label="产品品名规格" :show-overflow-tooltip="true" align="center" prop="moePmgg" width="280" />
          <el-table-column label="工单数量" align="center" prop="moeScsl" width="80" />
          <el-table-column label="入库数量" align="center" prop="moeRksl" width="80" />
          <el-table-column label="制造车间" align="center" prop="moeCj" width="80">
            <template v-slot="scope">
              <dict-tag :options="manufacturingPlantList" :value="scope.row.moeCj"/>
            </template>
          </el-table-column>
          <el-table-column label="物料代码" align="center" prop="modWldm" width="120" />
          <el-table-column label="材料编码" :show-overflow-tooltip="true" align="center" prop="modCustwldm" width="120" />
          <el-table-column label="材料品名规格" :show-overflow-tooltip="true" align="center" prop="modPmgg" width="280" />
          <el-table-column label="单位" align="center" prop="modDw" width="80">
            <template v-slot="scope">
              <dict-tag :options="unitList" :value="scope.row.modDw"/>
            </template>
          </el-table-column>
          <el-table-column label="单位用量" align="center" prop="bodDwyl" width="120" />
          <el-table-column label="应发料数量" align="center" prop="modLlsl" width="120" />
          <el-table-column label="实发料数量" align="center" prop="modYlsl" width="120" />
          <el-table-column label="超发料数量" align="center" prop="modCfsl" width="120" />
          <el-table-column label="发料节耗数量" align="center" prop="moeFljhsl" width="120" />
          <el-table-column label="入库产品折算材料数量" align="center" prop="moeZsclsl" width="155" />
          <el-table-column label="入库损耗数量" align="center" prop="moeRkshsl" width="120" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="注塑车间" name="second">
        <el-table
          border
          stripe
          :height="tableH"
          :data="materialSavingTotalZ"
          v-loading="loading2"
          @scrollBottom="load1"
          :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="制造车间" align="center" prop="moeCj" width="80">
            <template v-slot="scope">
              <dict-tag :options="manufacturingPlantList" :value="scope.row.moeCj"/>
            </template>
          </el-table-column>
          <el-table-column label="物料代码" align="center" prop="modWldm" width="120" />
          <el-table-column label="材料编码" :show-overflow-tooltip="true" align="center" prop="modCustwldm" width="120" />
          <el-table-column label="材料品名规格" :show-overflow-tooltip="true" align="center" prop="modPmgg" width="400" />
          <el-table-column label="单位" align="center" prop="modDw" width="80">
            <template v-slot="scope">
              <dict-tag :options="unitList" :value="scope.row.modDw"/>
            </template>
          </el-table-column>
          <el-table-column label="单位用量" align="center" prop="bodDwyl" width="120" />
          <el-table-column label="应发料数量" align="center" prop="modLlsl" width="120" />
          <el-table-column label="实发料数量" align="center" prop="modYlsl" width="120" />
          <el-table-column label="超发料数量" align="center" prop="modCfsl" width="120" />
          <el-table-column label="发料节耗数量" align="center" prop="moeFljhsl" width="120" />
          <el-table-column label="入库产品折算材料数量" align="center" prop="moeZsclsl" width="155" />
          <el-table-column label="入库损耗数量" align="center" prop="moeRkshsl" width="120" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="喷涂车间" name="third">
        <el-table
          border
          stripe
          :height="tableH"
          :data="materialSavingTotalP"
          v-loading="loading3"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="制造车间" align="center" prop="moeCj" width="80">
            <template v-slot="scope">
              <dict-tag :options="manufacturingPlantList" :value="scope.row.moeCj"/>
            </template>
          </el-table-column>
          <el-table-column label="物料代码" align="center" prop="modWldm" width="120" />
          <el-table-column label="材料编码" :show-overflow-tooltip="true" align="center" prop="modCustwldm" width="120" />
          <el-table-column label="材料品名规格" :show-overflow-tooltip="true" align="center" prop="modPmgg" width="400" />
          <el-table-column label="单位" align="center" prop="modDw" width="80">
            <template v-slot="scope">
              <dict-tag :options="unitList" :value="scope.row.modDw"/>
            </template>
          </el-table-column>
          <el-table-column label="单位用量" align="center" prop="bodDwyl" width="120" />
          <el-table-column label="应发料数量" align="center" prop="modLlsl" width="120" />
          <el-table-column label="实发料数量" align="center" prop="modYlsl" width="120" />
          <el-table-column label="超发料数量" align="center" prop="modCfsl" width="120" />
          <el-table-column label="发料节耗数量" align="center" prop="moeFljhsl" width="120" />
          <el-table-column label="入库产品折算材料数量" align="center" prop="moeZsclsl" width="155" />
          <el-table-column label="入库损耗数量" align="center" prop="moeRkshsl" width="120" />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="丝印车间" name="fourth">
        <el-table
          border
          stripe
          :height="tableH"
          :data="materialSavingTotalS"
          v-loading="loading4"
          @scrollBottom="load3"
          :header-cell-style="{ 'text-align': 'center' }">
          <el-table-column label="制造车间" align="center" prop="moeCj" width="80">
            <template v-slot="scope">
              <dict-tag :options="manufacturingPlantList" :value="scope.row.moeCj"/>
            </template>
          </el-table-column>
          <el-table-column label="物料代码" align="center" prop="modWldm" width="120" />
          <el-table-column label="材料编码" :show-overflow-tooltip="true" align="center" prop="modCustwldm" width="120" />
          <el-table-column label="材料品名规格" :show-overflow-tooltip="true" align="center" prop="modPmgg" width="400" />
          <el-table-column label="单位" align="center" prop="modDw" width="80">
            <template v-slot="scope">
              <dict-tag :options="unitList" :value="scope.row.modDw"/>
            </template>
          </el-table-column>
          <el-table-column label="单位用量" align="center" prop="bodDwyl" width="120" />
          <el-table-column label="应发料数量" align="center" prop="modLlsl" width="120" />
          <el-table-column label="实发料数量" align="center" prop="modYlsl" width="120" />
          <el-table-column label="超发料数量" align="center" prop="modCfsl" width="120" />
          <el-table-column label="发料节耗数量" align="center" prop="moeFljhsl" width="120" />
          <el-table-column label="入库产品折算材料数量" align="center" prop="moeZsclsl" width="155" />
          <el-table-column label="入库损耗数量" align="center" prop="moeRkshsl" width="120" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  exportMaterialSavingDetail,
  exportMaterialSavingTotal,
  listMaterialSavingDetail,
  listMaterialSavingTotal
} from "@/api/money/materialSaving";

export default {
  name: 'MaterialSaving',
  data() {
    return {
      // 遮罩层
      loading1: true,
      loading2: true,
      loading3: true,
      loading4: true,
      exportLoading1: false,
      exportLoading2: false,
      exportLoading3: false,
      exportLoading4: false,
      tableH: document.body.clientHeight - 300,
      activeTab: 'first',
      materialSavingDetail: [],
      materialSavingTotalZ: [],
      materialSavingTotalP: [],
      materialSavingTotalS: [],
      unitList: [],
      manufacturingPlantList: [],
      // 显示搜索条件
      openSearch: false,
      showSearch: true,
      // 总条数
      total1: 0,
      total2: 0,
      total3: 0,
      total4: 0,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      date: this.$moment().subtract(1, "months"),
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        year: this.$moment(this.date).format('yyyy'),
        month: this.$moment(this.date).format('MM'),
        marketingOrder: null,
        manufacturingOrder: null,
        param: null,
        completeParam: null
      },
      // 注塑车间
      queryParams1: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
      },
      // 喷涂车间
      queryParams2: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
      },
      // 丝印车间
      queryParams3: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
      },
      // 表单参数
      form: {},
    }
  },
  created() {
    this.getAllDicts("sys_unit").then(res => {
      this.unitList = res.data
    })
    this.getAllDicts("Manufacturing_plant").then(res => {
      this.manufacturingPlantList = res.data
    })
    this.getList()
  },
  methods: {
    /** 查询不良品统计分析列表 */
    getList() {
      this.loading1 = true
      this.loading2 = true
      this.loading3 = true
      this.loading4 = true
      this.queryParams.year = this.$moment(this.date).format('yyyy')
      this.queryParams.month = this.$moment(this.date).format('MM')
      listMaterialSavingDetail(this.queryParams)
        .then((response) => {
          // 明细
          this.materialSavingDetail = response.rows
          this.total1 = response.total
          this.loading1 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading1 = false
        })
      let queryParamsTemp = JSON.parse(JSON.stringify(this.queryParams))
      queryParamsTemp.workshop = 'mp_002'
      listMaterialSavingTotal(queryParamsTemp)
        .then((response) => {
          // 注塑车间
          this.materialSavingTotalZ = response.rows
          this.total2 = response.total
          this.loading2 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading2 = false
        })
      queryParamsTemp.workshop = 'mp_005'
      listMaterialSavingTotal(queryParamsTemp)
        .then((response) => {
          // 喷涂车间
          this.materialSavingTotalP = response.rows
          this.total3 = response.total
          this.loading3 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading3 = false
        })
      queryParamsTemp.workshop = 'mp_004'
      listMaterialSavingTotal(queryParamsTemp)
        .then((response) => {
          // 丝印车间
          this.materialSavingTotalS = response.rows
          this.total4 = response.total
          this.loading4 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading4 = false
        })
    },
    // 滚动加载
    load1() {
      if (this.queryParams1.pageNum < Math.ceil(this.total2 / this.queryParams1.pageSize)) {
        this.loading2 = true
        this.queryParams1.pageNum++
        let queryParams = JSON.parse(JSON.stringify(this.queryParams))
        queryParams.pageNum = this.queryParams1.pageNum
        queryParams.workshop = 'mp_002'
        listMaterialSavingTotal(queryParams)
          .then((response) => {
            // 注塑车间
            this.materialSavingTotalZ = this.materialSavingTotalZ.concat(response.rows)
            this.loading2 = false
          })
          .catch((error) => {
            console.log(error)
            this.loading2 = false
          })
      }
      return 0
    },
    // 滚动加载
    load2() {
      if (this.queryParams2.pageNum < Math.ceil(this.total3 / this.queryParams2.pageSize)) {
        this.loading3 = true
        this.queryParams2.pageNum++
        let queryParams = JSON.parse(JSON.stringify(this.queryParams))
        queryParams.pageNum = this.queryParams2.pageNum
        queryParams.workshop = 'mp_005'
        listMaterialSavingTotal(queryParams)
          .then((response) => {
            // 喷涂车间
            this.materialSavingTotalP = this.materialSavingTotalP.concat(response.rows)
            this.loading3 = false
          })
          .catch((error) => {
            console.log(error)
            this.loading3 = false
          })
      }
      return 0
    },
    // 滚动加载
    load3() {
      if (this.queryParams3.pageNum < Math.ceil(this.total4 / this.queryParams3.pageSize)) {
        this.loading4 = true
        this.queryParams3.pageNum++
        let queryParams = JSON.parse(JSON.stringify(this.queryParams))
        queryParams.pageNum = this.queryParams3.pageNum
        queryParams.workshop = 'mp_004'
        listMaterialSavingTotal(queryParams)
          .then((response) => {
            // 丝印车间
            this.materialSavingTotalS = this.materialSavingTotalS.concat(response.rows)
            this.loading4 = false
          })
          .catch((error) => {
            console.log(error)
            this.loading4 = false
          })
      }
      return 0
    },
    handleSearch() {
      this.openSearch = true
      this.title = '查询月度材料节耗报表'
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.openSearch = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
      this.openSearch = false
    },
    // 表单重置
    reset() {
      this.queryParams.marketingOrder = null
      this.queryParams.manufacturingOrder = null
      this.queryParams.param = null
      this.resetForm('form')
    },
    pageChange1(page) {
      this.loading1 = true
      this.queryParams.pageNum = page
      listMaterialSavingDetail(this.queryParams)
        .then((response) => {
          // 明细
          this.materialSavingDetail = response.rows
          this.total1 = response.total
          this.loading1 = false
        })
        .catch((error) => {
          console.log(error)
          this.loading1 = false
        })
    },
    handleExportDetail() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有月度材料节耗明细数据？', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'info',
      })
        .then(() => {
          this.exportLoading1 = true
          return exportMaterialSavingDetail(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading1 = false
        })
        .catch(() => {})
    },
    handleExportTotal1() {
      let queryParams = JSON.parse(JSON.stringify(this.queryParams))
      queryParams.workshop = 'mp_002'
      this.$confirm('是否确认导出所有注塑车间统计数据？', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'info',
      })
        .then(() => {
          this.exportLoading2 = true
          return exportMaterialSavingTotal(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading2 = false
        })
        .catch(() => {})
    },
    handleExportTotal2() {
      let queryParams = JSON.parse(JSON.stringify(this.queryParams))
      queryParams.workshop = 'mp_005'
      this.$confirm('是否确认导出所有喷涂车间统计数据？', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'info',
      })
        .then(() => {
          this.exportLoading3 = true
          return exportMaterialSavingTotal(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading3 = false
        })
        .catch(() => {})
    },
    handleExportTotal3() {
      let queryParams = JSON.parse(JSON.stringify(this.queryParams))
      queryParams.workshop = 'mp_004'
      this.$confirm('是否确认导出所有丝印车间统计数据？', '确认', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'info',
      })
        .then(() => {
          this.exportLoading4 = true
          return exportMaterialSavingTotal(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
          this.exportLoading4 = false
        })
        .catch(() => {})
    },
  },
}
</script>

<style lang="scss" scoped>

</style>
